var appPath = getAppPath();


$(function(){
	$("#carType").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1004',
		valueField:'id',
		textField:'text',
		onLoadSuccess:function(){
			$("#carType").combobox("setValue","");
		}
	});
	$("#carCondition").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1005',
		valueField:'id',
		textField:'text',
		onLoadSuccess:function(){
			$("#carCondition").combobox("setValue","");
		}
	});
	$("#delFlg").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1016',
		valueField:'id',
		textField:'text',
		value:"0"
	});
	initCarFileGrid();
});

function initCarFileGrid(){
	
	$("#carFileGrid").datagrid({
		url:appPath+'/statistics/carFile_pageList.do',
		height:'full',
		striped: true,
		remoteSort:false,
		singleSelect:true,
		idField:'carFileId',
		queryParams: getQueryParam(),
		columns:[[
		          {field:'carFileId',title:'操作',width:32,align:'center',
					  formatter:function(value,rowData,rowIndex){
			    		  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/images/system/icon/order-search.png' title='查看' onClick='showDetail("+rowIndex+")'/></a>";
					  }
		          },
		          {field:'licencePlateNo',title:'车牌号',width:120,align:'left',sortable:true},
		          {field:'carFrameNo',title:'车辆识别代号',width:160,align:'left',sortable:true},
		          {field:'carTypeName',title:'车辆类型',width:80,align:'left',sortable:true},
		          {field:'carLength',title:'车辆长度',width:80,align:'left',sortable:true},
		          {field:'carCapacity',title:'车辆载重',width:80,align:'left',sortable:true},
		          {field:'carAddress',title:'车辆所在地',width:80,align:'left',sortable:true},
		          {field:'carConditionName',title:'车体状况',width:100,align:'left',sortable:true},
		          {field:'validStateName',title:'有效状态',width:80,align:'left',sortable:true},
		          {field:'scanTimes',title:'浏览次数',width:100,align:'left',sortable:true},
		          {field:'createUser',title:'创建者',width:160,align:'left',sortable:true},
		          {field:'createTime',title:'创建时间',width:160,align:'left',sortable:true,formatter:fmtCreateTime},
		          {field:'updateUser',title:'更新者',width:160,align:'left',sortable:true},
		          {field:'updateTime',title:'更新时间',width:160,align:'left',sortable:true,formatter:fmtUpdateTime},
		          {field:'delFlg',title:'是否删除',width:80,align:'left',sortable:true,formatter:fmtDelFlg}
		        ]],
        pagination:true,
		rownumbers:true,
		onBeforeEdit:function(index,rowData){
			rowData.editing = true;
			$('#carFileGrid').datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,rowData){
			rowData.editing = false;
			$('#carFileGrid').datagrid('refreshRow', index);
		},
		onAfterEdit:function(index,rowData){
			rowData.editing = false;
			$('#carFileGrid').datagrid('refreshRow', index);
		},
		onSelect:function(index,rowData){
			showCode(rowData.codeTypeId);
		}
	});
	
}


function keyPressQuery(et){
	if (et.keyCode) {
		if (et.keyCode == 13)
			doQuery();
	} else {
		if (et.which == 13)
			doQuery();
    }  
}


/**
 * 展现详细界面
 * @param {} rowData
 */
function showDetail(rowIndex){
	
	//清理内容
	var rowData = $("#carFileGrid").datagrid("getRows")[rowIndex];
	$('#detailForm').form('clear');
	$('#detailForm').form('load',rowData);
	$('#detailDlg').window({
			title:'车辆档案信息',
			iconCls:'icon-orderdivide',
			modal: true,
			shadow: false,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
	$('#detailDlg').window('move',{left:20,top:20});
	$('#detailDlg').window('open');
}

//清除已填查询内容
function doClear(){
	$('#licencePlateNo').val("");
	$('#carFrameNo').val("");
	$('#carAddress').val("");
	$('#carType').combobox("select","");
	$('#carCondition').combobox("select","");
	$('#delFlg').combobox("select","");
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	queryObject.licencePlateNo = $("#licencePlateNo").val();
	queryObject.carFrameNo = $("#carFrameNo").val();
	queryObject.carAddress = $("#carAddress").val();
	queryObject.carType = $("#carType").combobox("getValue");
	queryObject.carCondition = $("#carCondition").combobox("getValue");
	queryObject.delFlg = $("#delFlg").combobox("getValue");
	return queryObject;
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.createTime=formatDate("y/m/d h:i:s",value);
}

function fmtUpdateTime(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.updateTime=formatDate("y/m/d h:i:s",value);
}

function fmtDelFlg(value, rowData, rowIndex){
	if(value == 0 )
	  return "未删除";
	else
	  return "删除";
}

/**
 * 查询数据
 */
function doQuery(){
	$("#carFileGrid").datagrid('unselectAll');
	$("#carFileGrid").datagrid('load',getQueryParam());
}

//时间格式转换
function formatDate(formatStr, fdate)
{
 var fTime, fStr = 'ymdhis';
 if (!formatStr)
  formatStr= "y-m-d h:i:s";
 if (!fdate)
	 return "";
 else
	 fTime = new Date(fdate);
 var formatArr = [
 fTime.getFullYear().toString(),
 (fTime.getMonth()+1).toString(),
 fTime.getDate().toString(),
 fTime.getHours().toString(),
 fTime.getMinutes().toString(),
 fTime.getSeconds().toString() 
 ];
 for (var i=0; i<formatArr.length; i++)
 {
  formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
 }
 return formatStr;
}